package com.thzy.bbs.callBacks;

import android.os.Handler;
import android.os.Message;

import com.alibaba.fastjson.JSON;
import com.thzy.bbs.common.Constants;
import com.thzy.bbs.entity.FrontResult;

import org.xutils.common.Callback;
import org.xutils.http.app.RequestInterceptListener;
import org.xutils.http.request.UriRequest;

/**
 * 登陆回调
 *
 * @author Lirui
 * @date 2017/1/13 9:56
 */

public class LoginCallBack implements RequestInterceptListener, Callback.CommonCallback<String> {
    private Handler handler;
    private Message message = new Message();

    public LoginCallBack(Handler handler) {
        this.handler = handler;
    }

    @Override
    public void beforeRequest(UriRequest request) throws Throwable {

    }

    @Override
    public void afterRequest(UriRequest request) throws Throwable {

    }

    @Override
    public void onSuccess(String result) {
        message.obj = result;
        handler.sendMessage(message);
    }

    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
        message.what = Constants.Code.LOGIN_ERROR;
        message.obj = JSON.toJSONString(new FrontResult(false, Constants.Code.REQUEST_ERROR));
        handler.sendMessage(message);
    }

    @Override
    public void onCancelled(CancelledException cex) {

    }

    @Override
    public void onFinished() {

    }
}
